<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/31
 * Time: 10:58
 */

namespace app\api\home;


class Cart extends Common
{
    // 加入购物车操作
    public function add()
    {
        $param = input('post.');
        if(empty($param)){
            return _json(-1, '参数不能为空');
        }
        if(empty($param['user_id'])){
            return _json(401, '请先登录');
        }
        $goods_info = db('goods')->where('id',$param['goods_id'])->find();
        //根据godos_type与key验证价格
        if($param['goods_type']){
            //有模型
            $spec_info = db('spec_price')->where('goods_id',$param['goods_id'])->where('key',$param['key'])->find();//规格信息
            if(!$spec_info){
                return _json(-1, '规格错误');
            }
            $goods_price = $spec_info['price'];
            $key_name = $spec_info['key_name'];
            if($param['number']>$spec_info['store_num']){
                return _json(-1, '库存不足');
            }
        }else{
            //无模型
            $key_name = '';
            $goods_price = $goods_info['shop_price'];
            if($param['number']>$goods_info['store_num']){
                return _json(-1, '库存不足');
            }
        }
        $data = [
            'user_id' => $param['user_id'],
            'goods_id' => $param['goods_id'],
            'goods_name' => $goods_info['goods_name'],
            'goods_thumb' => $goods_info['thumb_url'],
            'goods_num' => $param['number'],
            'key' => $param['key'],
            'key_name' => $key_name,
            'goods_price' => $goods_price,
            'remark' => '',
            'add_time' => time()
        ];
        //查询购物车是否拥有这件商品
        $has = db('cart')->where('goods_id',$param['goods_id'])->where('key',$param['key'])->find();
        if($has){
            $res = db('cart')->where('goods_id',$param['goods_id'])->where('key',$param['key'])->setInc('goods_num',$param['number']);
        }else{
            $res = db('cart')->data($data)->insert();
        }

        $num = db('cart')->where('user_id',$param['user_id'])->count();
        if($res){
            return _json(0, '成功加入购物车',['cartCount'=>$num]);
        }else{
            return _json(-1, '加入购物车失败');
        }

    }

    // 获取购物车商品数量
    public function goodscount()
    {
        $user_id = input('get.user_id',0);
        if(!$user_id){
            return _json(401, '请先登录');
        }
        $num = db('cart')->where('user_id',$user_id)->count();
        return _json(0, '执行成功',['cartCount'=>$num]);
    }

    // 获取购物车列表
    public function lists()
    {
        $user_id = input('get.user_id',0);
        if(!$user_id){
            return _json(401, '请先登录');
        }
        $cart_list = db('cart')->where('user_id',$user_id)->order('add_time desc')->select();
        foreach ($cart_list as $k=>$v){
            $cart_list[$k]['goods_thumb'] = 'http://'.$host = $_SERVER['HTTP_HOST'].get_file_path($v['goods_thumb']);
        }
        $data = [
            'cartList' => $cart_list
        ];
        return _json(0, '执行成功',$data);
    }

    // 批量删除购物车商品
    public function delete()
    {
        $checkedIds = input('post.checkedIds','');
        $user_id = input('post.user_id',0);
        if(!$user_id){
            return _json(401, '请先登录');
        }
        if(empty($checkedIds)){
            return _json(0, '执行成功');
        }
        $checkedIds = explode(',',$checkedIds);
        $res = db('cart')->whereIn('id',$checkedIds)->where('user_id',$user_id)->delete();
        if($res){
            return _json(0, '执行成功');
        }else{
            return _json(-1, '删除失败');
        }


    }

    // 改变购物车商品选中状态
    public function changeItemSelected()
    {
        $id = input('param.id');
        $selected = input('param.selected');
        CartModel::changeItemSelected($id, $selected);
        return _json('2000','', '成功');
    }

    // 改变购物车商品数量
    public function update()
    {
        $id = input('post.id',0);
        $number = input('post.number',0);
        if(!$id || !$number){
            return _json(0, '执行成功');
        }
        $res = db('cart')->where('id',$id)->update(['goods_num'=>$number]);
        if($res){
            return _json(0, '执行成功');
        }else{
            return _json(-1, '更新失败');
        }
    }

    /**
     * 判断购物车是否存在当前商品
     * 存在则数量加
     * @param $user_id
     * @param $goods_id
     * @param $spec_key
     * @param $add_num
     * @return bool|int|true
     */
    private function goodsNumIsAdd($user_id, $goods_id, $spec_key, $add_num)
    {
        if(CartModel::goodsExist($user_id, $goods_id, $spec_key)){
            return CartModel::addGoodsNum($user_id, $goods_id, $spec_key, $add_num);
        }else{
            return false;
        }
    }

}